package com.hb.trade.server.flow.manager;

import cn.hutool.core.util.StrUtil;
import org.fzzn.core.model.enums.LogicEnum;
import com.hb.core.pojo.flow.dto.FlowTempTaskDTO;
import com.hb.core.pojo.flow.entity.FlowFormField;
import com.hb.core.pojo.flow.ex.EmptyExpressionException;

import java.util.Collection;

public class FlowTempTaskManager extends FlowTempTaskDTO {
    /**
     * 计算审批节点
     *
     * @param fields
     * @return 返回true时为当前节点审批
     */
    public boolean eval(Collection<FlowFormField> fields) {
        // 定义流程时需要对比的字段
        String formField = this.getFormField();
        if (StrUtil.isEmpty(formField)) {
            return false;
        }
        // 计算表单实际输入值
        for (FlowFormField field : fields) {
            if (formField.equals(field.getFieldName())) {
                return compare(this.getCompareValue(), field.getFieldValue());
            }
        }
        return false;
    }

    /**
     * 计算逻辑
     *
     * @param compareValue
     * @param fieldValue
     * @return
     */
    private boolean compare(String compareValue, String fieldValue) {
        Integer logicValue = this.getLogicValue();
        LogicEnum logicEnum = LogicEnum.of(logicValue);
        if (logicEnum == null) {
            throw new EmptyExpressionException();
        }
        switch (logicEnum) {
            case GE:
                return fieldValue.compareTo(compareValue) >= 0;
            case GT:
                return fieldValue.compareTo(compareValue) > 0;
            case LE:
                return fieldValue.compareTo(compareValue) <= 0;
            case LT:
                return fieldValue.compareTo(compareValue) < 0;
            case EQ:
                return fieldValue.compareTo(compareValue) == 0;
            default:
        }
        return false;
    }
}
